import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:tree_hole/letters/LetterPaperPage.dart';
import 'package:tree_hole/ReplyPage.dart';
import 'TreeHoleList.dart';
import 'TreeHolePage.dart';

//树洞详情（各个回复头像、时间未加、树洞时间未加、图片加载延迟待解决）
class SingleTreeHolePage extends StatefulWidget {
  final String token;
  var article;
  String result;
  SingleTreeHolePage({this.token,this.article,Key key,this.result}) : super(key: key);

  @override
  _SingleTreeHolePage createState() => new _SingleTreeHolePage();
}
class _SingleTreeHolePage extends State<SingleTreeHolePage> {
  String result1='0';
List commentList;
  void initState() {
    super.initState();
    TreeHoleList.getArticleComments(widget.article.id).then((value) => setState((){
      commentList=value;
    })).whenComplete(() {
      print("comment异步任务处理完成");
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Container(
          child: Text('详情',
            style: TextStyle(
                color: Colors.black,
                letterSpacing: 5
            ),
          ),
        ),
        centerTitle:true,
        backgroundColor: Colors.white,
        //titleSpacing: 20,
        leading: IconButton(
          icon:Icon(Icons.arrow_back,color: Colors.black),
          onPressed: () {Navigator.pop(context);//if(widget.result=='回复成功')Navigator.popUntil(context,ModalRoute.withName('/TreeHolePage'));
          },
        ),
        actions: [
          IconButton(icon: Icon(Icons.reply,color: Colors.black87,),
            onPressed: () async {
              await Navigator.push(context, new MaterialPageRoute(builder: (context) => ReplyPage(widget.token,widget.article.id)));
              TreeHoleList.getArticleComments(widget.article.id).then((value) => setState((){
                commentList=value;
              }));
            },
          ),
          IconButton(icon: Icon(Icons.local_post_office_rounded,color: Colors.black87,),
            onPressed: () {Navigator.push(context, new MaterialPageRoute(builder: (context) => LetterPaperPage(widget.token)));},
          )
        ],
      ),
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: ListView(
          //mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(height: 10,),
            Column(
              children: [
                ListTile(
                  title: Align(alignment: Alignment.center,child: Text(
                    widget.article.title!=null?widget.article.title:'匿名树洞',
                  ),),
                  subtitle: Text(
                    widget.article.content,
                  ),
                ),
                Row(children: [
                  Spacer(),
                  Padding(padding: EdgeInsets.only(right: 10,bottom: 10),child: Text(widget.article.createTime),)]),
              ],
            ),
            SizedBox(width: double.infinity,
              child: Container(
                padding: EdgeInsets.all(5),
                color: Colors.white54,
                child: Text('回复',style: TextStyle(color: Colors.black)),
              ),
            ),
            commentList!=null?ListView.builder(
              itemCount:commentList.length,
              itemBuilder: (context,index1){
                return InkWell(
                  child: Column(
                    children: [
                      ListTile(
                        title:Text('匿名评论'),
                        subtitle: Text(commentList[index1]['content']),
                      ),
                      Row(children: [
                        Spacer(),
                        Padding(padding: EdgeInsets.only(right: 10,bottom: 10),child: Text(commentList[index1]['createTime']),)]),
                    ],
                  ),
                  onLongPress: (){
                    showDialog<Null>(
                        context: context,
                        barrierDismissible: true,
                        builder: (BuildContext context) {
                          return new AlertDialog(
                            title: new Text('你确定要删除该回复吗?', style: new TextStyle(fontSize: 17.0)),
                            actions: <Widget>[
                              new FlatButton(
                                child: new Text('取消'),
                                onPressed: (){
                                  Navigator.of(context).pop();
                                },
                              ),
                              new FlatButton(
                                child: new Text('确定'),
                                onPressed: () async {
                                  String result;
                                  result= await TreeHoleList.deleteComment(widget.token,commentList[index1]['id']);
                                  Fluttertoast.showToast(
                                      msg: result,
                                      toastLength: Toast.LENGTH_SHORT,
                                      gravity: ToastGravity.BOTTOM,
                                      timeInSecForIosWeb: 1,
                                      backgroundColor: Colors.amber[50],
                                      textColor: Colors.black,
                                      fontSize: 16.0
                                  );
                                  if(result=='删除成功'){
                                    TreeHoleList.getArticleComments(widget.article.id).then((value) => setState((){
                                      commentList=value;
                                    })).whenComplete(() {
                                      print("comment再次异步任务处理完成");
                                    });
                                    Navigator.pop(context);//此时点击返回会回到之前未评论的SingleTreeHolePage
                                  }
                                  else {Navigator.pop(context);}
                                },
                              )
                            ],
                          );
                        }
                    );
                  },
                );
              },
              shrinkWrap: true,
              physics: new NeverScrollableScrollPhysics(),
            ):Text('无'),
          ],
        ),
        decoration: BoxDecoration(
            image: new DecorationImage(
                fit: BoxFit.cover,
                image: new AssetImage('images/treeHole.jpg'))),
      ),
    );
  }
}